<template>
	<!--选择条款-->
	<el-dialog title="选择条款" :visible.sync="dialogVisible" size="small" :close-on-click-modal="false" :close-on-press-escape="false" :before-close="off">
		  <el-form label-width="150px" >
		  <el-row :span="24">
		  	<el-form-item label="当前险种对应条款" label-width="150px" :span="24">
				<el-select v-model="arrayKinds" multiple value-key="kindcode"  placeholder="条款代码" filterable size="small" style="width:80%">
					<el-option v-for="item in kindOptions" :key="item.kindcode" :label="item.kindcode + '-' + item.kindcname" :value="item"></el-option>
				</el-select>
			</el-form-item>
		  </el-row>
		</el-form>
		  <span slot="footer" class="dialog-footer">
			   <el-button @click="off">取 消</el-button>
			   <el-button type="danger" @click="reset">清 空</el-button>
			   <el-button type="warning" @click="select">确 定</el-button>
		  </span>
	</el-dialog>
</template>

<script>

import PlanAPI from "../../../../js/production/plan/planAPI";
export default {
	data() {
		return {
			dialogVisible: true,
			risk: null,
			arrayKinds: [],
			kindOptions: [],
      curGroupIndex: '',
		}
	},
	mounted() {
		this.risk = this.$parent.formData.risk;
    this.curGroupIndex = this.$parent.currentPropsIndex;
		PlanAPI.initRiskKindSelect(this);
		this.arrayKinds = this.$parent.formData.kindsGroup[this.curGroupIndex];
	},
	methods: {
		select() {
			if (this.$parent.formData.kinditemsGroup[this.curGroupIndex].length > 0) {
				this.$parent.cacheKindItems = Object.assign([], this.$parent.formData.kinditemsGroup[this.curGroupIndex]);
			}

			this.$parent.formData.kindcodesGroup[this.curGroupIndex] = '';
			this.$parent.formData.kindsGroup[this.curGroupIndex] = [];
			this.$parent.formData.kinditemsGroup[this.curGroupIndex] = [];
			this.$parent.formData.kindsGroup[this.curGroupIndex]= this.arrayKinds;
			if (this.arrayKinds.length < 1) {
				this.$parent.formData.itemsGroup[this.curGroupIndex] = [];
				this.$parent.formData.itemcodesGroup[this.curGroupIndex] = '';
				this.$parent.formData.kinditemsGroup[this.curGroupIndex] = [];
			} else {
				for(let i = 0; i <this.arrayKinds.length - 1; i++) {
					this.$parent.formData.kindcodesGroup[this.curGroupIndex] += this.arrayKinds[i].kindcode + ",";
				}
				if(this.arrayKinds.length > 0) {
					this.$parent.formData.kindcodesGroup[this.curGroupIndex] += this.arrayKinds[this.arrayKinds.length - 1].kindcode;
				}

				/**********************************************kindcodes改变后更新itemcodes***********************************************/
				let changedItems = [];
				for (let i = 0; i < this.arrayKinds.length; i++) {
					for(let j = 0; j < this.$parent.formData.itemsGroup[this.curGroupIndex].length; j++) {
						if (this.arrayKinds[i].kindcode === this.$parent.formData.itemsGroup[this.curGroupIndex][j].kindcode
                      && this.arrayKinds[i].kindversion === this.$parent.formData.itemsGroup[this.curGroupIndex][j].kindversion) {
							changedItems.push(this.$parent.formData.itemsGroup[this.curGroupIndex][j]);
						}
					}
				}
				this.$parent.formData.itemsGroup[this.curGroupIndex] = changedItems;
				this.$parent.formData.itemcodesGroup[this.curGroupIndex] = '';
				for(let i = 0; i < changedItems.length - 1; i++) {
					this.$parent.formData.itemcodesGroup[this.curGroupIndex] += changedItems[i].itemcode + ",";
				}
				if(changedItems.length > 0) {
					this.$parent.formData.itemcodesGroup[this.curGroupIndex] += changedItems[changedItems.length - 1].itemcode;
				}


				/*********************************************************************************************************************/
				//封装kinditem数据
				let kinds = this.$parent.formData.kindsGroup[this.curGroupIndex];
				for(let i = 0; i < kinds.length; i++) {
					let items = this.$parent.formData.itemsGroup[this.curGroupIndex];
					let hasItems = false;
					if (items.length > 0) {
						for(let j = 0; j <items.length; j++) {
							if (kinds[i].kindcode === items[j].kindcode && kinds[i].kindversion === items[j].kindversion) {
								hasItems = true;
								this.$parent.formData.kinditemsGroup[this.curGroupIndex].push({
									kindcode: kinds[i].kindcode,
									kindversion: kinds[i].kindversion,
									kindcname: kinds[i].kindcname,
									kindename: kinds[i].kindename,
									itemcode: items[j].itemcode,
									itemcname: items[j].itemcname,
									itemename: items[j].itemename,
									itemflag: items[j].itemflag,
									quantity : '1',
									currency : 'CNY',
									discount : '100.00000000',
									amount : '0.00',
									unitamount : '0.00',
									premium : '0.00',
									unitpremium : '0.00',
									rate: '0.00000000',
									calculateflag : kinds[i].calculateflag.substring(0,1),
									ownerFlag : '2'
								})
							}
						}
					}
					if (!hasItems) {
						this.$parent.formData.kinditemsGroup[this.curGroupIndex].push({
							kindcode: kinds[i].kindcode,
							kindversion: kinds[i].kindversion,
							kindcname: kinds[i].kindcname,
							kindename: kinds[i].kindename,
							itemcode: null,
							quantity : '1',
							currency : 'CNY',
							discount : '100.00000000',
							amount : '0.00',
							unitamount : '0.00',
							premium : '0.00',
							unitpremium : '0.00',
							rate: '0.00000000',
							calculateflag : kinds[i].calculateflag.substring(0,1),
							ownerFlag : '2'
						})
					}
				}

				/*********************************************************************************************************************/
				//更新kinditems记录, 替换为缓存cacheKindItems中与之匹配的数据
				let kinditems = this.$parent.formData.kinditemsGroup[this.curGroupIndex];
				let cacheKindItems = this.$parent.cacheKindItems;
				for (let i = 0; i < kinditems.length; i++) {
					for (let j = 0; j < cacheKindItems.length; j++) {
						if(kinditems[i].kindcode === cacheKindItems[j].kindcode && kinditems[i].itemcode === cacheKindItems[j].itemcode) {
							kinditems[i] = cacheKindItems[j];
							break;
						}
					}
				}
			}
			this.off();
		},
		reset() {
			this.arrayKinds = [];
			this.select();
		},
		off() {
			this.$parent.showRiskKindSelect2Dialog = false;
		}
	}
  }
</script>
